package com.bigshark.amine_admin.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bigshark.amine_admin.common.annocation.SysLog;
import com.bigshark.amine_admin.sys.model.SysDept;
import com.bigshark.amine_admin.sys.service.SysDeptService;
import com.bigshark.common.exception.AmineException;
import com.bigshark.common.utills.R;
import com.bigshark.common.utills.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys/dept")
public class SysDeptController extends AbstractController{
    @Autowired
    SysDeptService sysDeptService;

    @SysLog("添加部门")
    @PostMapping("/")
    public R create(@RequestBody SysDept dept){
        ValidatorUtils.validateEntity(dept);
        sysDeptService.save(dept);
        return R.ok();
    }

    @SysLog("删除部门")
    @DeleteMapping("/{id}")
    public R del(@PathVariable("id") Long id){
        //判断是都有子部门
        List<SysDept> list = sysDeptService.list(new QueryWrapper<SysDept>().eq("parent_id", id).eq("del_flag", 0));
        if(null !=list && list.size()>0){
            return R.error("请先删除子部门");
        }
        sysDeptService.removeById(id);
        return R.ok();
    }
    @SysLog("修改部门信息")
    @PutMapping("/{id}")
    public R upData(@PathVariable("id")Long id , SysDept dept){
        dept.setDeptId(id);
        if(!sysDeptService.saveOrUpdate(dept)){
            return R.error("修改失败");
        }
        return R.ok();
    }

    @GetMapping("/")
    public R list(){
        return R.ok();
    }

}
